package com.itggm.service.impl;

import cn.afterturn.easypoi.excel.ExcelExportUtil;
import cn.afterturn.easypoi.excel.ExcelImportUtil;
import cn.afterturn.easypoi.excel.entity.ExportParams;
import cn.afterturn.easypoi.excel.entity.ImportParams;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.ObjectUtils;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itggm.Result.ResponseResult;
import com.itggm.dto.ScoreExportDTO;
import com.itggm.dto.ScorePageDTO;
import com.itggm.pojo.Score;
import com.itggm.service.ScoreService;
import com.itggm.mapper.ScoreMapper;
import org.apache.poi.ss.usermodel.Workbook;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;

/**
* @author root
* @description 针对表【sys_score】的数据库操作Service实现
* @createDate 2024-12-30 11:03:16
*/
@Service
public class ScoreServiceImpl extends ServiceImpl<ScoreMapper, Score> implements ScoreService{

    @Resource
    private ScoreMapper scoreMapper;

    @Override
    public ResponseResult scoreList(ScorePageDTO scorePageDTO) {
        String clazz = scorePageDTO.getClazz();
        Date examTime = scorePageDTO.getExamTime();
        //新建分页对象
        Page<Score> scorePage = new Page<>(scorePageDTO.getPage(), scorePageDTO.getLimit());
        //构建分页条件
        LambdaQueryWrapper<Score> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(clazz), Score::getClazz, clazz)
                .eq(ObjectUtils.isNotNull(examTime), Score::getExamTime, examTime);
        //执行分页
        scoreMapper.selectPage(scorePage, wrapper);
        //封装返回结果
        List<Score> scoreList = scorePage.getRecords();
        return ResponseResult.success().data("tableData", scoreList)
                .data("total", scorePage.getTotal());

    }

    @Override
    public ResponseResult batchImport(MultipartFile file) {
        if (file.isEmpty()){
            return  ResponseResult.fail().message("请选择文件");
        }
        try {
            InputStream inputStream = file.getInputStream();

            ImportParams importParams = new ImportParams();
            importParams.setTitleRows(1);
            importParams.setHeadRows(1);
            List<Score> scores = ExcelImportUtil.importExcel(inputStream, Score.class, importParams);
            // 分批次添加数据 将多条数据 分割为多个数据块进行添加
            baseMapper.insert(scores,5);
            return  ResponseResult.success();
        } catch (Exception e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public void exportScore(ScoreExportDTO scoreExportDTO, HttpServletResponse response) {


        String clazz = scoreExportDTO.getClazz();
        Date examTime = scoreExportDTO.getExamTime();

        LambdaQueryWrapper<Score> lambda = new QueryWrapper<Score>().lambda();
        lambda.eq(StringUtils.isNotBlank(clazz),Score::getClazz,clazz)
                .eq(ObjectUtils.isNotNull(examTime),Score::getExamTime,examTime);

        List<Score> scores = baseMapper.selectList(lambda);

        ExportParams exportParams = new ExportParams();
        exportParams.setTitle("学生成绩表");
        exportParams.setSheetName("学生成绩表");

        Workbook workbook = ExcelExportUtil.exportExcel(exportParams, Score.class, scores);

        try {
            ServletOutputStream outputStream = response.getOutputStream();
            workbook.write(outputStream);
            outputStream.close();
            workbook.close();
        } catch (IOException e) {
            throw new RuntimeException(e);
        }
    }

    @Override
    public ResponseResult scoreVisual() {


        List<Score> scores =   baseMapper.scoreVisual();

        return ResponseResult.success().data("scoreList",scores);
    }
}




